home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 352_01.zip / STRPPSUB.CPP < prev    next >
C/C++ Source or Header  |  1993-04-10  |  918b  |  33 lines

  1. // STRPPSUB.CPP  - contains String::substring()
  2. //        routine to find a given string from within the 'this' string.
  3. //         creates a substring from position a for length b.
  4. //         if b not specified or less than 0, goes to end of string.
  5. //         NOTE: creates a new string, which must be explicitly deleted.
  6. //        obeys String::caseSens
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <alloc.h>
  10. #include <iostream.h>
  11. #include <ctype.h>
  12.  
  13.  
  14. #include "dblib.h"
  15.  
  16. String *String::substring(int a, int b)
  17.     {
  18.     String *tmp;
  19.     
  20.     tmp = new String;        // zero length for start.
  21.     int sn = n;
  22.     
  23.     if ( a<0 )  { a = 0; }        // validate substring limits in bounds.
  24.     if ( a>=sn ) { a = sn-1; }
  25.     if ( b<0 )  { b = sn-a; }    // if no length set, go to end of string.
  26.  
  27.     tmp->n = b;        
  28.     tmp->construct ( s+a );                // construct new string.
  29.     tmp->strpad();                        // pad with blanks.
  30.     
  31.     return tmp;
  32.     }        // end String::substring()
  33.